#include <xp/utl/fsm.h>
#include <xp/bas/stdio.h>
#include <xp/bas/string.h>

int xp_main (int argc, xp_char_t* argv[])
{
	xp_fsm_t* fsm;
	int flags = 0;
	xp_char_t* dir;

	if ((argc != 2 && argc != 3) ||
	    (argc == 3 && xp_strcmp(argv[1], XP_TEXT("-p")) != 0)) {
		xp_fprintf (xp_stderr, 
			XP_TEXT("usage: %s [-p] directory\n"), argv[0]);
		return -1;
	}

	if (argc == 3) {
		flags = XP_FSM_MKDIR_PARENTS;
		dir = argv[2];
	}
	else dir = argv[1];

	fsm = xp_fsm_open (XP_NULL);
	if (fsm == XP_NULL) {
		xp_fprintf (xp_stderr, XP_TEXT("error: cannot open fsm\n"));
		return -1;
	}

	if (xp_fsm_mkdir (fsm, dir, 0755, flags) == -1) {
		xp_fprintf (xp_stderr, 
			XP_TEXT("error: xp_fsm_mkdir - %s\n"), xp_fsm_errstr(fsm));
		xp_fsm_close (fsm);
		return -1;
	}

	xp_fsm_close (fsm);
	return 0;
}
